package com.oazon.moco.lan.handler;

import java.util.ArrayList;
import java.util.List;

import io.netty.channel.ChannelHandlerContext;
import android.support.v4.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.moco.launcher.LauncherApplication;
import com.moco.launcher.activity.MainActivity;
import com.moco.launcher.adapter.MainFlagmentAdapter;
import com.moco.launcher.fragment.MusicCategoryFragment;
import com.oazon.moco.bean.HttpMsgBodyBean;
import com.oazon.moco.bean.McServiceMsg;
import com.oazon.moco.lan.core.AbsHttpMsgHandler;
import com.oazon.moco.lan.core.controldriver.cache.ControlDriverCache;
import com.oazon.moco.lan.core.http.bean.JsonMessageType;
import com.oazon.moco.lan.core.http.bean.RequestClassification;
import com.oazon.moco.lan.core.http.bean.RequestOperation;
import com.oazon.moco.lan.core.http.bean.ZoneInfo;
import com.oazon.moco.lan.util.LanServer;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LanEventSource;
import com.oazon.sonos.core.AbsServiceHelper;
import com.oazon.sonos.core.ISonosSoap;
import com.oazon.sonos.core.SonosControler;
import com.oazon.sonos.util.SessionIdHelper;
import com.oazon.sonos.util.DeviceLinkHelper;

public class QueryHandler extends AbsHttpMsgHandler {

	@Override
	public void doBusiness(HttpMsgBodyBean bean) {
		if (!RequestOperation.OPERATION_QUERY.equals(bean.getOperation())) {
			return;
		}

		String uuid = bean.getUuid();
		String classification = bean.getClassification();
		String type = bean.getType();
		ChannelHandlerContext ctx = bean.getCtx();
		String data = bean.getData();

		// 如果没有订阅则不执行操作
		if (ControlDriverCache.getInstance().findControlDriverByUuid(uuid) == null) {
			throw new NullPointerException("uuid:" + uuid);
		}
		// 播放器类
		if (RequestClassification.CLASSIFICATION_AUDIO.equals(classification)) {

			// 获取播放状态
			if (JsonMessageType.GET_PLAY_STATUS.toString().equals(type)) {
				LinkSDK.getInstance().requestPlayStatus(
						new LanEventSource(ctx, uuid));
			}

			// 获取播放进度
			if (JsonMessageType.GET_POSITION_INFO.toString().equals(type)) {

				LinkSDK.getInstance().requestProgress(
						new LanEventSource(ctx, uuid), getInstanceID(data));
			}

			// 获取音量
			if (JsonMessageType.GET_VOLUME.toString().equals(type)) {
				LinkSDK.getInstance().requestVolume(
						new LanEventSource(ctx, uuid), getInstanceID(data));
			}

			// 获取当前播放的歌曲信息
			if (JsonMessageType.GET_SONG_INFO.toString().equals(type)) {
				LinkSDK.getInstance().requestSongInfo(
						new LanEventSource(ctx, uuid));
			}

			// 获取当前播放的目录信息
			if (JsonMessageType.GET_CATEGORY.toString().equals(type)) {
				LinkSDK.getInstance().requestCategory(
						new LanEventSource(ctx, uuid));
			}

			// 获取播放模式
			if (JsonMessageType.GET_PLAY_MODE.toString().equals(type)) {
				LinkSDK.getInstance().requestPlayMode(
						new LanEventSource(ctx, uuid));
			}

			// 初始化时获取完整的信息
			if (JsonMessageType.GET_ALL_STATUS.toString().equals(type)) {

				LinkSDK.getInstance().requestAllStatus(
						new LanEventSource(ctx, uuid), getInstanceID(data));
			}

		}
		// 系统级别的
		else if (RequestClassification.CLASSIFICATION_SYSTEM
				.equals(classification)) {

			// 查询版本号
			if (JsonMessageType.GET_ZONE_INFO.toString().equals(type)) {
				String strZoneInfo = data;
				ZoneInfo zoneInfo = JSON.parseObject(strZoneInfo,
						ZoneInfo.class);

				LinkSDK.getInstance().requestVersion(
						new LanEventSource(ctx, uuid), zoneInfo);
			}

		} else if (RequestClassification.CLASSIFICATION_SERVICE
				.equals(classification)) {
			if (JsonMessageType.LIST_SERVICE.toString().equals(type)) {

				List<McServiceMsg> mcServiceMsgs = new ArrayList<McServiceMsg>();
 
				MainFlagmentAdapter adapter = MainActivity.getInstance().mFlagmentAdapter;

				for (int i = 0; i < adapter.getCount(); i++) {
					Fragment fragment = adapter.getItem(i);
					if (fragment == null
							|| !(fragment instanceof MusicCategoryFragment)) {
						continue;
					}
					MusicCategoryFragment mFragment = (MusicCategoryFragment) fragment;
					McServiceMsg serviceMsg = mFragment.getServiceMsg();
					
					if (serviceMsg == null) {
						continue;	
					}
					mcServiceMsgs.add(serviceMsg);
				}
				String response = JSON.toJSONString(mcServiceMsgs);
				LanServer.responseResult(type, 0, response, ctx);
			}
		}
	}

}
